package hl.action;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;

import hl.action.BaseAction;
import hl.pageModel.DataGrid;
import hl.pageModel.Json;
import hl.pageModel.Stuff;
import hl.service.StuffService;

@Namespace("/")
@Action(value = "stuffAction")
public class StuffAction extends BaseAction implements ModelDriven<Stuff> {

	Stuff stuff = new Stuff();

	private StuffService stuffService;

	@Override
	public Stuff getModel() {
		return stuff;
	}

	public StuffService getStuffService() {
		return stuffService;
	}

	@Autowired
	public void setStuffService(StuffService stuffService) {
		this.stuffService = stuffService;
	}

	public void getNotRegStuff() {
		List l = (List) stuffService.getNotRegisterStuff();
		super.writeJson(l);
	}

	public void add() {
		Json j = new Json();
		try {
			stuffService.save(stuff);
			j.setSuccess(true);
			j.setMsg("保存成功");

		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}

	public void getDatagrid() {
		DataGrid d = stuffService.getDatagrid(stuff);
		super.writeJson(d);
	}

	public void edit() {
		Json j = new Json();
		try {
			stuffService.edit(stuff);
			j.setSuccess(true);
			j.setMsg("修改成功！");

		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}

	public void remove() {
		Json j = new Json();
		try {
			stuffService.remove(stuff);
			j.setSuccess(true);
			j.setMsg("删除成功！");

		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}
	
	public void getComboBox() {
		super.writeJson(stuffService.getComboBox());
	}
}
